package com.bnz.controller;

import com.bnz.pojo.*;
import com.bnz.service.ClassesService;
import com.bnz.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Autowired
    private ClassesService classesService;

    //1. 列表所有学生
    @GetMapping("list")
    public String findAll(Model model){
        List<Student> all = studentService.findAll();
        model.addAttribute("list",all);
        return "student/list";
    }

    @RequestMapping("findPage")
    public String findPage(@RequestParam(defaultValue = "1",required = false) Integer page,
                           @RequestParam(defaultValue = "5",required = false) Integer pageSize,
                           Model model){

        PageResult<Student> students=studentService.findPage(page,pageSize);
        model.addAttribute("pr",students);

        return "student/list";
    }
    @RequestMapping("search")
    public String search(@RequestParam(defaultValue = "1",required = false) Integer page,
                         @RequestParam(defaultValue = "3",required = false) Integer pageSize,
                         StudentVo vo,
                         Model model){

        PageResult<Student> pr=studentService.search(page,pageSize,vo);
        List<Classes> classes = classesService.findAll();
        model.addAttribute("pr",pr);
        model.addAttribute("classes",classes);
        model.addAttribute("stud",vo);
        return "student/list";
    }

    @PostMapping("add")
    public String add(Student student){
        studentService.add(student);
        return "redirect:/student/search";
    }

    @PostMapping("update")
    public String update(Student student){

        studentService.update(student);
        return "redirect:/student/search";
    }

    @GetMapping("delete/{sid}")
    public String datele(@PathVariable String sid){

        studentService.delete(sid);
        return "redirect:/student/search";
    }



}
